public class Solution {
    public int lengthOfLastWord(String s) {
        StringBuilder sb = new StringBuilder();

        for (int i = s.length() - 1; i > 0; i--) {
            if (s.charAt(i) != ' ') {
                sb.append(s.charAt(i));
                continue;
            }

            if (sb.length() > 0) {
                break;
            }
        }

        return sb.length();
    }

    public int lengthOfLastWord2(String s) {
        int length = 0;

        for (int i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) != ' ') {
                length++;
                continue;
            }

            if (length > 0) {
                break;
            }
        }

        return length;
    }
}